//
//  RootTabBarController.m
//  WorkFrame
//
//  Created by tiannu on 15/10/21.
//  Copyright (c) 2015年 tiannu. All rights reserved.
//
#define ClassName(_name) [NSClassFromString(@#_name) new]

#import "RootTabBarController.h"
#import "HomeViewController.h"
#import "LifeViewController.h"
#import "MallViewController.h"
#import "WorkViewController.h"
#import "MyViewController.h"
#import "WelComeLoginViewController.h"

@interface RootTabBarController ()

@property (nonatomic, strong) NSMutableArray *arrayTiele;
@end

@implementation RootTabBarController {
    UICustomTabbar *tabbar;
}

- (NSMutableArray *)arrayTiele
{
    if (!_arrayTiele) {
        _arrayTiele = [NSMutableArray array];
    }
    return _arrayTiele;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *dicObje = [Tool jsonFile:@"PersonalTab"];
    NSArray *tabbarT = [dicObje objectForKey:@"tabbar"];
    [self setVC:tabbarT];
    [self uiTabBarTitles:tabbarT];
}

- (void)setVC:(NSArray *)vcArray
{
    for (int i  = 0; i < vcArray.count; i++) {
        if (i<vcArray.count) {
            if (i == 0) {
                UINavigationController *firstNavigationController = [[UINavigationController alloc]
                                                                     initWithRootViewController:ClassName(HomeViewController)];
                [self.arrayTiele addObject:firstNavigationController];
            }
            if (i==1) {
                UINavigationController *firstNavigationController = [[UINavigationController alloc]
                                                                     initWithRootViewController:ClassName(LifeViewController)];
                [self.arrayTiele addObject:firstNavigationController];
            }
            if (i==2) {
                UINavigationController *firstNavigationController = [[UINavigationController alloc]
                                                                     initWithRootViewController:ClassName(MallViewController)];
                [self.arrayTiele addObject:firstNavigationController];
            }
            if (i == 3) {
                UINavigationController *firstNavigationController = [[UINavigationController alloc]
                                                                     initWithRootViewController:ClassName(WorkViewController)];
                [self.arrayTiele addObject:firstNavigationController];
            }
            if (i == 4) {
                UINavigationController *firstNavigationController = [[UINavigationController alloc]
                                                                     initWithRootViewController:ClassName(MyViewController)];
                [self.arrayTiele addObject:firstNavigationController];
            }
        }
    }
    [self setViewControllers:self.arrayTiele];
}
- (void)uiTabBarTitles:(NSArray*)title
{
    NSInteger counts = [[[self tabBar] items] count];
    tabbar = [UICustomTabbar customTabbar];
    //设置tabbar文字及图片
    NSMutableArray *titles = [NSMutableArray array];
    NSMutableArray *normalImages = [NSMutableArray array];
    NSMutableArray *selectedImages = [NSMutableArray array];
    //设置tabbar背景
    NSMutableArray *normalBackgroundImages = [NSMutableArray array];
    NSMutableArray *selectedBackgroundImages = [NSMutableArray array];
    for (int i = 0; i<title.count; i++) {
        NSDictionary *dic = title[i];
        NSString *strT = [dic objectForKey:@"title"];
        NSString *strN = [dic objectForKey:@"image_Nor"];
        NSString *strS = [dic objectForKey:@"image_sel"];
        NSString *strBgN = [dic objectForKey:@"tabbar_Bg_No"];
        NSString *strBgS = [dic objectForKey:@"tabbar_Bg_Se"];
        [titles addObject:strT];
        [normalImages addObject:strN];
        [selectedImages addObject:strS];
        [normalBackgroundImages addObject:strBgN];
        [selectedBackgroundImages addObject:strBgS];
    }
    tabbar.titles = titles;//tabbar文字
    tabbar.counts = counts;//tabbar长度
    tabbar.normalImages = normalImages;//tabbar图片(未选择)
    tabbar.selectedImages = selectedImages;//tabbar图片(以选择)
//    tabbar.backgroundColor = [UIColor clearColor];
    
//    tabbar.normalBackgroundImages = normalBackgroundImages;//tabbar背景图片(未选择)
//    tabbar.selectedBackgroundImages = selectedBackgroundImages;//tabbar背景图片(以选择)
    tabbar.textNormalColor = SHC_RGBA(136, 136, 136, 1);//tabbar字体颜色(未选择)
    tabbar.textSelectedColor = [UIColor redColor];//tabbar字体颜色(以选择)
    tabbar.delegate = self;
    [self.tabBar addSubview:tabbar];
}
//改变点击当前self.selectedIndex的值
- (void)tabBar:(UICustomTabbar *)tabBar didSelectItemAtIndex:(NSInteger)index {
    self.selectedIndex = index;
}
//点击tabbar 跳转
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    if (selectedIndex == 3) {
        WelComeLoginViewController *vc = [[WelComeLoginViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
        nav.modalPresentationStyle = 0;
        [self presentViewController:nav animated:YES completion:nil];
    }
    [super setSelectedIndex:selectedIndex];
    tabbar.selectedIndex = selectedIndex;
}
//点击转入如果需要登录跳转
- (void)showLoginVC:(NSUInteger)selectedIndex {
    __weak typeof(self) weakSelf = self;
    [Tool addLoginViewShow:self success:^{
        //判断是否绑定车型
//        if (![[UserInfoModel shareInstance]isBindingCar]) {
//            [self pushBangdingCar:selectedIndex];
//            return;
//        }
        weakSelf.selectedIndex = selectedIndex;
    }];
}
@end
